package com.classic.mafiacards;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Created by pavilion on 11/16/13.
 */
public class OptionsActivity extends PreferenceActivity
        implements SharedPreferences.OnSharedPreferenceChangeListener,
                   Preference.OnPreferenceClickListener {
    final int PLAYERS_NUMBER_PREF = 0;
    final int MAFIA_NUMBER_PREF = 1;
    final int SHOW_PLAYER_POS_PREF = 2;
    final int CHANGE_CARDS_SKIN_PREF = 3;
    final int LANGUAGE_PREF = 4;
    final int AUTHOR_PREF = 5;

    static int press_times;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OptionsReader.updateLanguage(this);
        addPreferencesFromResource(R.xml.options_screen);
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
        getPreferenceScreen().getPreference(AUTHOR_PREF).setOnPreferenceClickListener(this);

        getPreferenceScreen().getPreference(PLAYERS_NUMBER_PREF).setSummary(String.format(
                getString(R.string.preferences_current_number),
                PreferenceManager.getDefaultSharedPreferences(this).getString("players", "10"))
        );
        getPreferenceScreen().getPreference(MAFIA_NUMBER_PREF).setSummary(String.format(
                getString(R.string.preferences_current_number),
                PreferenceManager.getDefaultSharedPreferences(this).getString("mafia", "3"))
        );
        getPreferenceScreen().getPreference(LANGUAGE_PREF).setSummary(PreferenceManager.
                getDefaultSharedPreferences(this).getString("language", "English"));
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("language")) {
            OptionsReader.updateLanguage(this);
            setPreferenceScreen(null);
            addPreferencesFromResource(R.xml.options_screen);
            getPreferenceScreen().getPreference(PLAYERS_NUMBER_PREF).setSummary(String.format(
                    getString(R.string.preferences_current_number),
                    PreferenceManager.getDefaultSharedPreferences(this).getString("players", "10"))
            );
            getPreferenceScreen().getPreference(MAFIA_NUMBER_PREF).setSummary(String.format(
                    getString(R.string.preferences_current_number),
                    PreferenceManager.getDefaultSharedPreferences(this).getString("mafia", "3"))
            );
            getPreferenceScreen().getPreference(LANGUAGE_PREF).setSummary(PreferenceManager.
                    getDefaultSharedPreferences(this).getString("language","English"));
        }

        if (key.equals("players")) {
            getPreferenceScreen().getPreference(PLAYERS_NUMBER_PREF).setSummary(String.format(
                    getString(R.string.preferences_current_number),
                    PreferenceManager.getDefaultSharedPreferences(this).getString("players", "10"))
            );
        }

        if (key.equals("mafia")) {
            getPreferenceScreen().getPreference(MAFIA_NUMBER_PREF).setSummary(String.format(
                    getString(R.string.preferences_current_number),
                    PreferenceManager.getDefaultSharedPreferences(this).getString("mafia", "3"))
            );
        }
    }


    @Override
    public boolean onPreferenceClick(Preference preference) {
        press_times++;
        if(preference.getKey().equals("author") && press_times == 15) {
            Toast easter_egg = Toast.makeText(getApplicationContext(),
                    getString(R.string.author_name),
                    Toast.LENGTH_SHORT);
            LinearLayout easterEggView = (LinearLayout) easter_egg.getView();
            ImageView author_image = new ImageView(this);
            author_image.setImageResource(R.drawable.author);
            easterEggView.addView(author_image, 0);
            easter_egg.show();
            press_times = 0;
        }
        return false;
    }

    public void onBackPressed() {
        startActivity(new Intent(this, MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
        finish();
    }
}